<template>
  <el-dialog
      :model-value="dialogVisible"
      :title="props.dialogJyTitle"
      width="40%"
      @close="handleClose"
  >
    <el-form :model="form" ref="formRef" label-width="140px" :rules="rules">
      <el-form-item label="客户名称" prop="name">
          <el-select v-model="form.name" clearable placeholder="请选择" style="width: 60%;">
          <el-option
                  v-for="(item, index) in KhName"
                  :key="index"
                  :label="item"
                  :value="item"
          />
          </el-select>
      </el-form-item>
      <el-form-item label="交易金额" prop="money">
        <el-input v-model="form.money" clearable/>
      </el-form-item>
      <el-form-item label="交易产品" prop="product">
          <el-select v-model="form.product" clearable placeholder="请选择" style="width: 60%;">
              <el-option
                      v-for="(item, index) in CpName"
                      :key="index"
                      :label="item"
                      :value="item"
              />
          </el-select>
      </el-form-item>
      <el-form-item label="交易数量" prop="number">
        <el-input v-model="form.number" clearable/>
      </el-form-item>
      <el-form-item label="图片名称">
        <el-input disabled v-model="tpmc" />
      </el-form-item>
      <el-form-item label="交易图片上传" prop="picture">
        <my-upload
            :file-type="'jysj'"
            @get-form-pic-value="getFormPicValue"
        >
        </my-upload>
      </el-form-item>
      <el-form-item label="交易日期" prop="time">
        <el-date-picker
            v-model="form.time"
            type="date"
            placeholder="选择日期"
            :size="'default'"
            clearable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {defineEmits, ref, defineProps, watch, onMounted} from 'vue'
import {ElNotification} from "element-plus";
import MyUpload from "@/components/commons/myUpload.vue";
import {getOptionInfo} from "@/api/methods/initInfo.js";
import {getCpmc} from "@/api/apiPath/twoPart.js";
import {getKhName} from "@/api/apiPath/fourPart.js";

const tpmc = ref('')
const getFormPicValue = (value)=> {
  console.log(value)
  form.value.picture = value.pic
  tpmc.value = value.tpmc
}
const props = defineProps({
  dialogJyTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogJyTableValue: {
    type: Object,
    default: {}
  },
  queryForm: {
  },
  dialogVisible: {
    type: Boolean,
    default: false
  }
})

const formRef = ref(null)

const form = ref({
  name: '',
  money: '',
  product: '',
  number: '',
  picture: '',
  time: '',
})
const CpName = ref([])
const KhName = ref([])
onMounted(async ()=> {
    KhName.value = await getOptionInfo(getKhName())
    CpName.value = await getOptionInfo(getCpmc())
    form.value = props.dialogJyTableValue
})

const rules = ref({
  name: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  product: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  number: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
})
const emits = defineEmits(['update:modelValue', 'handleConfirm'])
const handleClose = ()=> {
  emits('update:modelValue', false)
}
const handleConfirm = () => {
  emits("handleConfirm", {...form.value}, {...formRef.value})
}

const picureUpLoadSuccess = (e)=> {
  console.log(e)
  ElNotification({
    message: "图片上传成功！",
    type: 'success',
    duration: 1500
  })
}
const picureUpLoadFail = (e)=> {
  console.log(e)
  ElNotification({
    message: "图片上传失败~",
    type: 'error',
    duration: 1500
  })
}
</script>

<style scoped>


</style>